<!DOCTYPE html>
<html xmlns:layout="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
	layout:decorate="~{common/layoutGroup}">

<head>
	<meta charset="UTF-8">
	<title>资讯信息</title>
</head>

<body>
	<div layout:fragment="fragment_body">
		<el-row :gutter="20">
			<el-col :span="24">
				<el-button type="primary" @click="openRaceInfo(null)">添 加</el-button>
				<el-table :data="raceInfos" border highlight-current-row style="width: 100%">
					<!-- -->
					<el-table-column prop="infoImg" label="封面">
						<template slot-scope="scope">
							<img v-if="scope.row.infoImg" :src="scope.row.infoImg" class="avatar-sm">
						</template>
					</el-table-column>
					<el-table-column prop="infoTitle" label="标题"></el-table-column>
					<el-table-column prop="infoSubtitle" label="副标题"></el-table-column>
					<!--<el-table-column prop="infoContent" label="内容"></el-table-column>-->
					<el-table-column prop="infoTime" label="创建时间"></el-table-column>
					<el-table-column label="操作" width="160px">
						<template slot-scope="scope">
							<el-button @click="openRaceInfo(scope.row)" type="text" size="small">修 改</el-button>
							<el-popconfirm title="是否删除？" @confirm="delRaceInfo(scope.row.infoId)">
								<el-button slot="reference" type="text" size="small">删 除</el-button>
							</el-popconfirm>
						</template>
					</el-table-column>
				</el-table>
			</el-col>
		</el-row>
		<!--弹框-->
		<el-dialog :title="raceInfoForm.ttitle" :visible.sync="raceInfoForm.visible" width="30%">
			<el-form :model="raceInfoForm" :rules="raceInfoRules" ref="raceInfoForm" label-width="100px">
				<!-- -->
				<el-form-item label="标题" prop="infoTitle">
					<el-input v-model="raceInfoForm.infoTitle"></el-input>
				</el-form-item>
				<el-form-item label="副标题" prop="infoSubtitle">
					<el-input v-model="raceInfoForm.infoSubtitle"></el-input>
				</el-form-item>
				<el-form-item label="封面" prop="infoImg">
					<el-upload class="avatar-uploader" action="/file/upload" :show-file-list="false"
						:on-success="handleUploadSuccess" :before-upload="beforeUploadImg">
						<img v-if="raceInfoForm.infoImg" :src="raceInfoForm.infoImg" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="内容" prop="infoContent">
					<el-input v-model="raceInfoForm.infoContent"
					type="textarea" :autosize="{ minRows: 5, maxRows: 10}"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="closeRaceInfo">取 消</el-button>
				<el-button type="primary" @click="saveRaceInfo">保 存</el-button>
			</span>
		</el-dialog>
	</div>
</body>
<script type="text/javascript" layout:fragment="fragment_script">
	let vueData = {
		raceInfos: [],
		selectRaceInfo: null,
		raceInfoForm: {
			visible: false,
			ttitle: '新增/修改',
			infoId : '',
			infoPublishId : '',
			infoTitle : '',
			infoSubtitle : '',
			infoContent : '',
			infoImg : '',
			infoTime : '',
		},
		raceInfoQuery: {
			page: '',
			size: '',
		},
		raceInfoRules: {
			infoTitle: [
				{required: true, message: '不能为空', trigger: 'blur'}
			],
			infoSubtitle: [
				{required: true, message: '不能为空', trigger: 'blur'}
			],
			infoContent: [
				{required: true, message: '不能为空', trigger: 'blur'}
			],
			infoImg: [
				{required: true, message: '不能为空', trigger: 'blur'}
			]
		},
	}
	let vueMethods = {
		getRaceInfos() {
			let that = this
			postRequst('/getInfos', that.raceInfoQuery, true).then(function (resp) {
				that.raceInfos = resp.rows
				if (resp.rows && resp.rows.length > 0) {
					that.selectRaceInfo = resp.rows[0]
				}
			})
		},
		openRaceInfo(row) {
			let that = this
			if (row) {
				that.raceInfoForm.ttitle = '修改'
				that.raceInfoForm.infoId = row.infoId
				that.raceInfoForm.infoPublishId = row.infoPublishId
				that.raceInfoForm.infoTitle = row.infoTitle
				that.raceInfoForm.infoSubtitle = row.infoSubtitle
				that.raceInfoForm.infoContent = row.infoContent
				that.raceInfoForm.infoImg = row.infoImg
			} else {
				that.raceInfoForm.ttitle = '新增'
				that.raceInfoForm.infoId = ''
				that.raceInfoForm.infoPublishId = ''
				that.raceInfoForm.infoTitle = ''
				that.raceInfoForm.infoSubtitle = ''
				that.raceInfoForm.infoContent = ''
				that.raceInfoForm.infoImg = ''
			}
			that.raceInfoForm.visible = true
		},
		closeRaceInfo() {
			this.$refs["raceInfoForm"].resetFields();
			this.raceInfoForm.visible = false
		},
		saveRaceInfo() {
			let that = this
			this.$refs["raceInfoForm"].validate((valid) => {
				if (valid) {
					var url = '/createInfo'
					if(that.raceInfoForm.infoId){
						url = '/upodateInfo'
					}
					postRequst(url, that.raceInfoForm).then(function (resp) {
						if (resp.status == 0) {
							that.getRaceInfos()
							that.raceInfoForm.visible = false
							that.$message({message: resp.msg, type: "success"});
						} else {
							that.$message({message: resp.msg, type: "error"});
						}
					})
				}
			});
		},
		delRaceInfo(infoId) {
			let that = this
			postRequst('/delRaceInfo', {infoId: infoId}, true).then(function (resp) {
				if (resp.status == 0) {
					that.getRaceInfos()
					that.$message({message: resp.msg, type: "success"});
				} else {
					that.$message({message: resp.msg, type: "error"});
				}
			})
		},
		handleUploadSuccess(res, file) {
			let imageUrl = URL.createObjectURL(file.raw);
			this.raceInfoForm.infoImg = res.data
		}
	}
	function vueCreated() {
		let that = this
		that.getRaceInfos()
	}
</script>

</html>